package com.manage.project.biz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringFacility;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringFacilityService;
import com.manage.project.system.domain.SysNotice;
import com.manage.project.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.project.biz.domain.BizYingjijianxiu;
import com.manage.project.biz.service.IBizYingjijianxiuService;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.web.page.TableDataInfo;

/**
 * 土建结构经常检查Controller
 *
 * @author liutao

 */
@RestController
@RequestMapping("/biz/yingjijianxiu")
public class CivilEngineeringFrequentlyInspectionController extends BaseController
{
    @Autowired
    private IBizYingjijianxiuService bizYingjijianxiuService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;
    /**
     * 查询土建结构经常检查列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:yingjijianxiu:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizYingjijianxiu bizYingjijianxiu)
    {
        startPage();
        List<BizYingjijianxiu> list = bizYingjijianxiuService.selectBizYingjijianxiuList(bizYingjijianxiu);
        return getDataTable(list);
    }

    /**
     * 导出土建结构经常检查列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:yingjijianxiu:export')")
    @Log(title = "土建结构经常检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizYingjijianxiu bizYingjijianxiu)
    {
        List<BizYingjijianxiu> list = bizYingjijianxiuService.selectBizYingjijianxiuList(bizYingjijianxiu);
        ExcelUtil<BizYingjijianxiu> util = new ExcelUtil<BizYingjijianxiu>(BizYingjijianxiu.class);
        return util.exportExcel(list, "土建结构经常检查数据");
    }

    /**
     * 获取土建结构经常检查详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:yingjijianxiu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizYingjijianxiuService.selectBizYingjijianxiuById(id));
    }

    /**
     * 新增土建结构经常检查
     */
//    @PreAuthorize("@ss.hasPermi('biz:yingjijianxiu:add')")
    @Log(title = "土建结构经常检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizYingjijianxiu bizYingjijianxiu)
    {
       /* String id=bizYingjijianxiu.getField1();
        Long id1= Long.valueOf(id);
        BizSuidao bizSuidao1=iBizSuidaoService.selectBizSuidaoById(id1);*/
//        String roadId= bizYingjijianxiu.getField5();
        String tunnelId= bizYingjijianxiu.getField1();
        Long id1= Long.valueOf(tunnelId);
        BizSuidao bizSuidao1=iBizSuidaoService.selectBizSuidaoById(id1);
        String tunneId= bizSuidao1.getField1();
        String tunneName=bizYingjijianxiu.getField2();
        String roadName=bizYingjijianxiu.getField4();
        String mileageNumber=bizYingjijianxiu.getField8();
        String tunnelName=bizYingjijianxiu.getField2();
        String facility=bizYingjijianxiu.getField9();
        String time=bizYingjijianxiu.getField7();
        Date date=new Date();
        bizYingjijianxiu.setField1(tunneId);
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunneName,tunneId);
        if (bizSuidao==null){
            return AjaxResult.error("您输入的隧道名称、道路名称和隧道编码不匹配，请重新输入");
        }
        CivilEnginneeringFacility civilFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunneName,facility,mileageNumber);
        if(civilFacility==null){
            return AjaxResult.error(tunneName+"里程桩号"+mileageNumber+"没有安装"+facility+",请联系管理员");
        }
       /* SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(time);*/
        if("一般异常".equals(bizYingjijianxiu.getField13())){
            /*添加专项检查任务*/
            SysNotice sysNotice= new SysNotice();
            sysNotice.setNoticeTitle("土建结构-专项检查");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("经常检查提交："+roadName+"-"+tunnelId+tunnelName+"-"+mileageNumber+facility+"检查任务");

//            sysNotice.setNoticeTitle(tunnelName+"土建结构"+"“"+facility+"”"+"专项检查任务");
//            sysNotice.setNoticeType("1");
//            sysNotice.setStatus("0");
//            sysNotice.setNoticeContent("请于"+time+"之后24h内完成此项检查任务");
            sysNotice.setCreateBy(getUsername());
            sysNotice.setCreateTime(date);
            noticeService.insertNotice(sysNotice);
            /*添加 保养维修任务*/
            sysNotice.setNoticeTitle(tunnelName+"土建结构"+"“"+facility+"”"+"保养维修任务");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("请于"+time+"之后24h内完成"+tunnelName+"保养维修工作");
            sysNotice.setCreateBy(getUsername());
            sysNotice.setCreateTime(date);
            noticeService.insertNotice(sysNotice);
        }
        if("情况不明".equals(bizYingjijianxiu.getField12())){
            SysNotice sysNotice= new SysNotice();
            sysNotice.setNoticeTitle(tunnelName+"土建结构"+"“"+facility+"”"+"定期检查任务");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("请于"+time+"之后24h内完成此项检查任务");
            sysNotice.setCreateBy(getUsername());
            sysNotice.setCreateTime(date);
            noticeService.insertNotice(sysNotice);
        }

        return toAjax(bizYingjijianxiuService.insertBizYingjijianxiu(bizYingjijianxiu));
    }

    /**
     * 修改土建结构经常检查
     */
    @Log(title = "土建结构经常检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizYingjijianxiu bizYingjijianxiu)
    {
        String tunnelId= bizYingjijianxiu.getField1();
        String tunneName=bizYingjijianxiu.getField2();
        String roadName=bizYingjijianxiu.getField4();
        String mileageNumber=bizYingjijianxiu.getField8();
        String facility=bizYingjijianxiu.getField9();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunneName,tunnelId);
        if (bizSuidao==null){
            return AjaxResult.error("您输入的隧道名称、道路名称和隧道编码不匹配，请重新输入");
        }
        CivilEnginneeringFacility civilFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunneName,facility,mileageNumber);
        if(civilFacility==null){
            return AjaxResult.error(tunneName+"里程桩号"+mileageNumber+"没有安装"+facility+",请联系管理员");
        }
        return toAjax(bizYingjijianxiuService.updateBizYingjijianxiu(bizYingjijianxiu));
    }

    /**
     * 删除土建结构经常检查
     */
//    @PreAuthorize("@ss.hasPermi('biz:yingjijianxiu:remove')")
    @Log(title = "土建结构经常检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizYingjijianxiuService.deleteBizYingjijianxiuByIds(ids));
    }
}
